package cn.itcast.tant_test;

/**
 * @ClassName TestFunctionalInterface
 * @Description TODO
 * @Author tant
 * @Date 2025/5/16 9:46
 */
public class TestFunctionalInterface {

    public static void main(String[] args) {
        ShowInterface s = (str) -> System.out.println(str);
        s.show("hello world");

        CalInterface c = (a, b) -> a + b;
        System.out.println(c.cal(1, 2));

        //函数式接口作为参数
        callShow("hello world", (str) -> System.out.println(str));

        //函数式接口作为参数
        System.out.println(callCal(1, 2, (a, b) -> a + b));

        //函数式接口作为返回值
        ShowInterface s1 = getShow();
        s1.show("hello world");

        //函数式接口作为返回值
        CalInterface c1 = getCal();
        System.out.println(c1.cal(1, 2));
    }

    private static void callShow(String something, ShowInterface s) {
        s.show(something);
    }

    private static int callCal(int a, int b, CalInterface c) {
        return c.cal(a, b);
    }

    private static ShowInterface getShow() {
        return (str) -> System.out.println(str);
    }


    private static CalInterface getCal() {
        return (a, b) -> a + b;
    }

}


@FunctionalInterface // 函数式接口
interface ShowInterface {
    void show(String s);
}

@FunctionalInterface // 函数式接口
interface CalInterface {
    int cal(int a, int b);
}


